##########################################################################################

library(data.table)
library(optparse)
library(Seurat)
library(ggthemes)
library(dplyr)
library(ggsci)

##########################################################################################
option_list <- list(
    make_option(c("--rna_file"), type = "character"),
    make_option(c("--gene_list_file"), type = "character"),
    make_option(c("--out_path"), type = "character") 
)

if(1!=1){
    ## 单细胞表达文件
    rna_file <- "~/20231121_singleMuti/results/qc_atac_v3/all/testis_combined.annotationCellType.qc.Rdata"

    ## 认为关键的TF
    gene_list_file <- "~/20231121_singleMuti/config/Human_reported_TF2.csv"

    ## 输出
    out_path <- "~/20231121_singleMuti/results/report_tf"

}

###########################################################################################
parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

rna_file <- opt$rna_file
gene_list_file <- opt$gene_list_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

###########################################################################################

grp_order2 = c("SSC",
"Differenting&Differented SPG",
"Leptotene",
"Zygotene",
"Patchytene",
"Diplotene",
"Early stage of spermatids",
"Round&ElongateS.tids",
"Sperm",
"Leydig cells",
"Myoid cells",
"Pericytes",
"Sertoli cells",
"Endothelial cells",
"NKT cells",
"Macrophages")

use_colors <- c(pal_npg("nrc")(10) , pal_jco("default")(6))
#names(use_colors) <- unique(scrnat$cell_type)
names(use_colors) <-c( "Myoid cells","Leydig cells" ,          
"Endothelial cells","Zygotene",         
"Round&ElongateS.tids","Patchytene",
"SSC","Sperm" ,                 
"Diplotene","Early stage of spermatids", 
"Leptotene","Sertoli cells",            
"Macrophages","Differenting&Differented SPG",
"Pericytes","NKT cells")

names(use_colors) <- paste0( as.numeric(factor( names(use_colors) , levels = grp_order2 , order = T )) , names(use_colors))

###########################################################################################
## 导入数据
a <- load(rna_file)
## scrnat
DefaultAssay(scrnat) <- "MAGIC_RNA"
scrnat$cell_type <- factor( scrnat$cell_type , levels = grp_order2[grp_order2 %in% unique(scrnat$cell_type)] , order = T )
## 确认排序
Idents(scrnat) <- scrnat$cell_type

gene_list <- data.frame( fread(gene_list_file , header = F) )$V1

###########################################################################################
## rna测到的基因
rna_gene <- rownames(scrnat@assays$MAGIC_RNA)
gene_list <- gene_list[gene_list %in% rna_gene]

for( gene in gene_list ){
    ## rna表达
    p4 <- FeaturePlot(scrnat, features = c(gene))

    ## rna表达的小提琴图
    p4_violin <- VlnPlot(scrnat, features = c(gene) , pt.size = 0) + FontSize(x.text = 6, y.text = 6 , x.title = 7, y.title = 7) + NoLegend()

    ## 输出
    pdf(paste0(out_path, "/" , gene , "_plotEmbedding.RNA.pdf"), width=5, height=5)
    print(p4)
    print(p4_violin)
    dev.off()
}